var get=require('../../../utils/getRequest');
var post=require('../../../utils/postRequest');
const api= require('../../../utils/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:null, 
    container:null,
    dialogShow:false,
    dialogMessage:'',
    dialogTitle:'',
    enduserReplenishmanList:[],
    enduserReplenishmanSize:0,
    joinReplenishmanList:[],
    joinReplenishmanSize:0,
    overlayShow:false,
    invitationCode:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      var type=options.type;
      this.setData({type:type});
      console.log("type============="+type)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setData({
      container: () => wx.createSelectorQuery().select('#container'),
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
      var type=this.data.type;
      var that=this;
      if(type=="1"){
          this.getReplenishmanList();
      }else{
        this.getJoinReplenishmanList();
      }
  },
  getJoinReplenishmanList(){
    console.log("AAAAAAAAAAAAAAAA")
    var that=this;
    var data={
      openId:api.openId
    }
    wx.showLoading({title:'努力加载中'})
    get.getRequest(null,api.getJoinReplenishmanList,data).then(res=>{
        console.log(res);
        that.setData({joinReplenishmanList:res.data,
                      joinReplenishmanSize:res.data.length
                    })
    }).finally(res=>{
      wx.hideLoading();
    });
  },
  getReplenishmanList(){
    console.log("BBBBBBBBBBBBBBBBBBBBBBBB")
    var that=this;
    var data={
      openId:api.openId
    }
    wx.showLoading({title:'努力加载中'})
    get.getRequest(null,api.getEnduserReplenishmanList,data).then(res=>{
        console.log(res);
        that.setData({enduserReplenishmanList:res.data,
                      enduserReplenishmanSize:res.data.length})
    }).finally(res=>{
      wx.hideLoading();
    });
  },
  createInvitationCode(){
    var that=this;
    var data={
      openId:api.openId
    }
    wx.showLoading({title:'努力加载中'})
      get.getRequest(null,api.createEnduserInvitationCode,data).then(res=>{
            if(res.code==200){
              var invitationCode=res.data.invitationCode;
              that.setData({
                dialogTitle:'生成邀请码',
                dialogMessage:invitationCode+'\n十分钟内有效',
                dialogShow:true,
              });
            }
      }).finally(res=>{
        wx.hideLoading();
      })
  },
  deleteByTeamId(e){
      var that=this;
      var teamId=e.currentTarget.dataset.teamid;
      var name=e.currentTarget.dataset.name;
     console.log(e);
 
      wx.showModal({
        title: '删除提醒',
        content: '删除补货员：'+name,
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            that.savedelReplenishman(teamId);
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
  },
  savedelReplenishman(teamId){
    var that=this;
    var data={teamId:teamId}
    wx.showLoading({title:'努力加载中'})
    get.getRequest(null,api.deleteEnduserReplenishmanByTeamId,data).then(res=>{
        if(res.code==200){
          wx.showModal({
            title: '删除提醒',
            content: '删除成功',
            showCancel:false,
            success (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                that.getReplenishmanList();
              } 
            }
          })
          
        }
    }).finally(res=>{
        wx.hideLoading();
      })
  },
  becomeReplenishman(){
    var that=this;
    
    var invitationCode=this.data.invitationCode;
    if(invitationCode==null||invitationCode==""){
      wx.showToast({
        title: '激活码不能为空',
        icon: 'error',
        duration: 1000
      })
      return;
    }
    console.log("invitationCode======="+invitationCode)
    var data={openId:api.openId,invitationCode:invitationCode}
    wx.showLoading({title:'努力加载中'})
    get.getRequest(null,api.bingReplenishmanByInvitationcode,data).then(res=>{
        if(res.code==200){
          wx.showModal({
            title: '绑定成功',
            showCancel:false,
            content: res.msg,
            success (res) {
              if (res.confirm) {
                that.getJoinReplenishmanList();
              } 
            }
          })
        }else{
          wx.showModal({
            title: '绑定失败',
            showCancel:false,
            content: res.msg
          })
        }
    }).finally(res=>{
      wx.hideLoading();
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})